package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class ResumeClient {


	
	public String updateResume(String str) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", str);					
		try {
			String resultXml = WSUtils.callWS("saveResume",params, Constants.ResumeURL);			
			return resultXml;
		} catch (Exception e) {
			throw  e;
		}
	}


	
	public String getResume(String mobile) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", mobile);					
		try {
			String resultXml = WSUtils.callWS("getResume",params, Constants.ResumeURL);			
			return resultXml;
		} catch (Exception e) {
			throw  e;
		}
	}
	
	public Map<String,String> pullXml(String resultXml) throws Exception {	
		Map map=new HashMap<String,Object>();
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else if(resultXml.equals("N")){
			map.put("empty", "empty");
			return map;
		}else{				
				try {
					ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
					InputStreamReader in = new InputStreamReader(bin);
					XmlPullParser parser = Xml.newPullParser();
					parser.setInput(in);
					int event = parser.getEventType();
					while (event != XmlPullParser.END_DOCUMENT) {				
						switch (event) {
						case XmlPullParser.START_DOCUMENT:
							map=new HashMap<String,Object>();
							break;
						case XmlPullParser.START_TAG:					
							if ("resumeId".equals(parser.getName())) {
								map.put("resumeId", parser.nextText());
							} else if ("name".equals(parser.getName())) {
								map.put("name", parser.nextText());
							} else if ("tel".equals(parser.getName())) {
								map.put("tel", parser.nextText());
							} else if ("sex".equals(parser.getName())) {
								map.put("sex", parser.nextText());
							} else if ("state".equals(parser.getName())) {
								map.put("state", parser.nextText());
							} else if ("ll".equals(parser.getName())) {
								map.put("ll", parser.nextText());
							} else if ("qq".equals(parser.getName())) {
								map.put("qq", parser.nextText());
							}else if ("line".equals(parser.getName())) {
								map.put("line", parser.nextText());
							}else if ("date".equals(parser.getName())) {
								map.put("date", parser.nextText());
							} if ("lineName".equals(parser.getName())) {
								map.put("lineName", parser.nextText());
							}
							
							
							break;				
						}
						event = parser.next();
					}
				} catch (Exception e) {
					e.printStackTrace();
					throw new Exception("解析失败！", e);
				}
				return map;
		}
		
		
	   
	}

}
